package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;


@Cmd(code = Protocol.S_UNION_QUERY_MASTER_NAME, desc = "查询商会会长昵称")
public class QueryMasterNameCmd extends AbstractCommand {

	private String masterName = "";

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		String unionUid = player.getUserInfo().getUnionUid();
		if(StringUtils.isNullOrEmpty(unionUid)){//未加入商会
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}
		UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionUid);
		if(null == unionInfo || unionInfo.isDelete()){
			ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
			return;
		}
		CrossUnionMember crossUnionMember = CrossUnionMgr.getUnionMember(unionUid, unionInfo.getMasterUserId());
		if(null != crossUnionMember){
			masterName = crossUnionMember.getUserBaseInfo().getNickName();
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		UnionProto.UnionQueryMasterNameRespMsg.Builder respMsg = UnionProto.UnionQueryMasterNameRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret){
			respMsg.setMasterName(masterName);
		}
		player.sendPacket(Protocol.U_UNION_QUERY_MASTER_NAME, respMsg);
	}

}
